package com.wjh.kt_order.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wjh.kt_order.service.OrderDetailService;
import com.wjh.kt_order.service.OrderInfoService;
import com.wjh.kt_order.mapper.OrderInfoMapper;
import com.wjh.kt_vod.model.order.OrderDetail;
import com.wjh.kt_vod.model.order.OrderInfo;
import com.wjh.kt_vod.vo.order.OrderInfoQueryVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 */
@Service
public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo>
    implements OrderInfoService{

    @Autowired
    private OrderDetailService orderDetailService;

    @Override
    public Map<String, Object> selectOrderInfoPage(Page<OrderInfo> page, OrderInfoQueryVo orderInfoQueryVo) {

        Long userId = orderInfoQueryVo.getUserId();
        String outTradeNo = orderInfoQueryVo.getOutTradeNo(); //订单交易编号（第三方支付用)
        String phone = orderInfoQueryVo.getPhone();
        String createTimeBegin = orderInfoQueryVo.getCreateTimeBegin();
        String createTimeEnd = orderInfoQueryVo.getCreateTimeEnd();
        Integer orderStatus = orderInfoQueryVo.getOrderStatus();

        LambdaQueryWrapper<OrderInfo> orderInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(userId)){
            orderInfoLambdaQueryWrapper.eq(OrderInfo::getUserId,userId);
        }
        if (!StringUtils.isEmpty(outTradeNo)){
            orderInfoLambdaQueryWrapper.eq(OrderInfo::getOutTradeNo,outTradeNo);
        }
        if (!StringUtils.isEmpty(phone)){
            orderInfoLambdaQueryWrapper.eq(OrderInfo::getPhone,phone);
        }
        if (!StringUtils.isEmpty(createTimeBegin)){
            orderInfoLambdaQueryWrapper.ge(OrderInfo::getCreateTime,createTimeBegin);
        }
        if (!StringUtils.isEmpty(createTimeEnd)){
            orderInfoLambdaQueryWrapper.le(OrderInfo::getCreateTime,createTimeEnd);
        }
        if (!StringUtils.isEmpty(orderStatus)){
            orderInfoLambdaQueryWrapper.le(OrderInfo::getOrderStatus,orderStatus);
        }

        Page<OrderInfo> orderInfoPage1 = baseMapper.selectPage(page, orderInfoLambdaQueryWrapper);
        List<OrderInfo> records = orderInfoPage1.getRecords();
        long total = orderInfoPage1.getTotal();//总记录数
        long pages = orderInfoPage1.getPages();//总页数

        records.stream().forEach(item ->{
           this.getOrderDetail(item);
        });
        HashMap<String, Object> stringObjectHashMap = new HashMap<>();
        stringObjectHashMap.put("total",total);
        stringObjectHashMap.put("records",records);
        stringObjectHashMap.put("pageCount",pages);
        return stringObjectHashMap;
    }

    private OrderInfo getOrderDetail(OrderInfo item) {
//        订单id
        Long id = item.getId();
        OrderDetail orderDetail= orderDetailService.getById(id);
        if (!StringUtils.isEmpty(orderDetail)){
            String courseName = orderDetail.getCourseName();
            item.getParam().put("courseName",courseName);
        }
        return item;
    }
}




